package com.qingmu.club.minaapi.wechat;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.qingmu.club.minaapi.utils.HttpUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class QrCodeUtil {


    public static String createQrCode(String staffShopId, String accessToken, String path){
        String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("scene", staffShopId);
        jsonObject.put("page", "pages/Club/index");
        jsonObject.put("is_hyaline", false);
        jsonObject.put("width", 430);
        jsonObject.put("auto_color", false);
        Map<String,Object> line_color = new HashMap<>();
        line_color.put("r", 0);
        line_color.put("g", 0);
        line_color.put("b", 0);
        jsonObject.put("line_color", line_color);

        String json = jsonObject.toJSONString();

        String s = HttpUtils.getInstance().downloadFile(path, url, json, null);

        return s;
    }


    private static final int BLACK = 0xff000000;
    private static final int WHITE = 0xFFFFFFFF;

    public static String createQrcode(String path, String content) {

        String filePostfix = "png";
        Long id = new Date().getTime();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String dateString = df.format(new Date());
        String newFilePath =  "/upload/miniqrcode/" + dateString + "/";
        String fileName = id + ".png";


        File checkFile = new File(path +newFilePath);
        if (!checkFile.exists() && !checkFile.isDirectory()) {
            checkFile.mkdirs();
        }

        String filePath = path +newFilePath + fileName;

        File file = new File(filePath);
        encode(content, file, filePostfix, BarcodeFormat.QR_CODE, 500, 500, null);

        String returnPath = "/miniqrcode/" + dateString + "/"+fileName;
        return returnPath;


    }

    public static void encode(String contents, File file, String filePostfix, BarcodeFormat format, int width, int height, Map<EncodeHintType, ?> hints) {
        try {
            contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
            writeToFile(bitMatrix, filePostfix, file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        ImageIO.write(image, format, file);
    }

    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
            }
        }
        return image;
    }


}
